package Server;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.dictionary.stopword.CoreStopWordDictionary;
import com.hankcs.hanlp.seg.Segment;
import com.hankcs.hanlp.seg.common.Term;
import com.hankcs.hanlp.tokenizer.NotionalTokenizer;
/**
 * 分词处理器，使用hanlp分词
 * @author Administrator
 *
 */
public class Segmentor {
	private Segment segment = HanLP.newSegment().enableCustomDictionary(true);
	//对一条工单进行分词
	private String singleSegment(String wostring){
		String[] s = wostring.split("\\+\\+");
		String id = s[0];
		String content = s[1];
		String time = s[2];
		String str = NotionalTokenizer.segment(content).toString();
		List<Term> seg = segment.seg(content);
		CoreStopWordDictionary.apply(seg);
		String result = id+"++"+seg.toString().substring(1,str.length()-1)+"++"+time;
		
		return result;
	}
	
	public List<String> listSegment(List<String> woList){
		List<String> result = new ArrayList<>();
		for(String s:woList){
			String segstring = singleSegment(s);
			if(segstring.split("\\+\\+")[1].isEmpty()||segstring.split("\\+\\+")[1].equals("")){
				continue;
			}
			result.add(segstring);
		}
		return result;
	}
	
	
	public static void main(String[] args) {
		Segmentor segmentor = new Segmentor();
		String sourcedatapath = "D:/workspace/HotEventMonitoring/2017年1月-2017年12月嘉兴的单子信息/save.txt";
		String segfile = "D:/workspace/HotEventMonitoring/2017年1月-2017年12月嘉兴的单子信息/seg.txt";
		List<String> seglist = segmentor.listSegment(IOHandler.readAsList(new File(sourcedatapath)));
		IOHandler.serialize2File(seglist, new File(segfile));
	}
}
